package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.BorrowAuthEnum;
import com.atguigu.srb.core.enums.BorrowerStatusEnum;
import com.atguigu.srb.core.enums.IntegralEnum;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.mapper.UserIntegralMapper;
import com.atguigu.srb.core.pojo.entity.*;
import com.atguigu.srb.core.mapper.BorrowerMapper;
import com.atguigu.srb.core.pojo.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerAttachVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerAttachService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author fan
 * @since 2021-11-22
 */
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {
    @Resource
    private BorrowerAttachService borrowerAttachService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserBindMapper userBindMapper;
    @Resource
    private DictService dictService;
    @Resource
    private UserIntegralMapper userIntegralMapper;
    @Resource UserInfoMapper userInfoMapper;
    @Override
    public void saveBorrower(BorrowerVO borrowerVO,String userId) {
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("user_id",userId);
        UserBind userBind = userBindMapper.selectOne(userBindQueryWrapper);
        String idCard = userBind.getIdCard();
        String mobile = userBind.getMobile();
        String name = userBind.getName();
        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerVO,borrower);
        borrower.setUserId(userId);
        borrower.setIdCard(idCard);
        borrower.setName(name);
        borrower.setMobile(mobile);

        borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        baseMapper.insert(borrower);


        List<BorrowerAttach> borrowerAttachList = borrowerVO.getBorrowerAttachList();
        borrowerAttachService.saveAttach(borrowerAttachList,userId);

        userInfoService.setStatus(userId);

    }

    @Override
    public Integer getInitStatus(String userId) {
        QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
        borrowerQueryWrapper.eq("user_id",userId);
        Borrower borrower = baseMapper.selectOne(borrowerQueryWrapper);
        Integer status = borrower.getStatus();
        return status;
    }

    @Override
    public Page<Borrower> getPageList(Long page, Long limit, String keyword) {
        Page<Borrower> borrowerPage = new Page<>();
        borrowerPage.setCurrent(page);
        borrowerPage.setSize(limit);
        QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(keyword)){
            // keyword有可能是名字，手机号，身份证
            borrowerQueryWrapper
                    .like("name",keyword)
                    .or()
                    .like("mobile",keyword)
                    .or()
                    .like("id_card",keyword);
        }
        Page<Borrower> pageModel = baseMapper.selectPage(borrowerPage, borrowerQueryWrapper);
        return pageModel;
    }

    @Override
    public Integer getStatus(Long id) {
        Borrower borrower = baseMapper.selectById(id);
        Integer status = borrower.getStatus();
        return status;
    }

    @Override
    public BorrowerDetailVO getBorrowerDetail(Long id) {
        Borrower borrower = baseMapper.selectById(id);

        String userId = borrower.getUserId();
        BorrowerVO borrowerVO = new BorrowerVO();
        BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
        BeanUtils.copyProperties(borrower, borrowerDetailVO);

        borrowerDetailVO.setMarry(borrower.getMarry()?"是":"否");
        borrowerDetailVO.setSex(borrower.getSex()==1?"男":"女");

        String education = dictService.getNameByParentDictCodeAndValue("education", borrower.getEducation());
        String industry = dictService.getNameByParentDictCodeAndValue("moneyUse", borrower.getIndustry());
        String income = dictService.getNameByParentDictCodeAndValue("income", borrower.getIncome());
        String returnSource = dictService.getNameByParentDictCodeAndValue("returnSource", borrower.getReturnSource());
        String contactsRelation = dictService.getNameByParentDictCodeAndValue("relation", borrower.getContactsRelation());

        borrowerDetailVO.setEducation(education);
        borrowerDetailVO.setIndustry(industry);
        borrowerDetailVO.setIncome(income);
        borrowerDetailVO.setReturnSource(returnSource);
        borrowerDetailVO.setContactsRelation(contactsRelation);

        //审批状态
        String status = BorrowerStatusEnum.getMsgByStatus(borrower.getStatus());
        borrowerDetailVO.setStatus(status);

        List<BorrowerAttachVO> borrowerAttachVO = borrowerAttachService.getImage(userId);
        borrowerDetailVO.setBorrowerAttachVOList(borrowerAttachVO);

        return borrowerDetailVO;
    }

    @Override
    public void saveApproval(BorrowerApprovalVO borrowerApprovalVO) {
        //修改状态
        Long id = borrowerApprovalVO.getBorrowerId();
        Borrower borrower1 = baseMapper.selectById(id);
        String userId = borrower1.getUserId();

        Borrower borrower = new Borrower();
        borrower.setStatus(borrowerApprovalVO.getStatus());
        QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
        borrowerQueryWrapper.eq("user_id",userId);

        baseMapper.update(borrower,borrowerQueryWrapper);



        //添加积分
        BigDecimal bigDecimal = new BigDecimal("0");
        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(userId);
        userIntegral.setContent("基本信息");
        userIntegral.setIntegral(borrowerApprovalVO.getInfoIntegral());

        bigDecimal = bigDecimal.add(new BigDecimal(borrowerApprovalVO.getInfoIntegral()));
        userIntegralMapper.insert(userIntegral);

        if (borrowerApprovalVO.getIsIdCardOk()){
            userIntegral.setContent("身份证信息");
            userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
            bigDecimal = bigDecimal.add(new BigDecimal(IntegralEnum.BORROWER_IDCARD.getIntegral()));
            userIntegralMapper.insert(userIntegral);
        };
        if (borrowerApprovalVO.getIsHouseOk()){
            userIntegral.setContent("房产信息");
            userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
            bigDecimal = bigDecimal.add(new BigDecimal(IntegralEnum.BORROWER_HOUSE.getIntegral()));
            userIntegralMapper.insert(userIntegral);
        };
        if (borrowerApprovalVO.getIsIdCardOk()){
            userIntegral.setContent("车辆信息");
            userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
            bigDecimal = bigDecimal.add(new BigDecimal(IntegralEnum.BORROWER_CAR.getIntegral()));
            userIntegralMapper.insert(userIntegral);
        };

        QueryWrapper<Borrower> borrowerQueryWrapper1 = new QueryWrapper<>();
        borrowerQueryWrapper1.eq("user_id",userId);
        Borrower borrower2 = baseMapper.selectOne(borrowerQueryWrapper1);

        UserInfo userInfo = new UserInfo();
        userInfo.setId(borrower2.getUserId());
        userInfo.setIntegral(bigDecimal.intValue());
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_OK.getStatus());
        userInfoMapper.updateById(userInfo);


    }
}
